/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.cloud.i18n;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.iwindplus.cloud.i18n.domain.property.I18nProperty;
import com.iwindplus.cloud.i18n.manager.I18nManager;
import com.iwindplus.cloud.i18n.support.NacosBundleMessageSource;
import jakarta.annotation.Resource;
import java.nio.charset.Charset;
import java.time.Duration;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * feign 配置.
 *
 * @author zengdegui
 * @since 2019/9/3
 */
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "spring.messages", name = "enabled-remote", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(I18nProperty.class)
public class I18nConfiguration {

    @Resource
    private I18nProperty property;

    @Resource
    private NacosConfigManager nacosConfigManager;

    /**
     * 创建 NacosBundleMessageSource.
     *
     * @return NacosBundleMessageSource
     */
    @Primary
    @Bean(name = "messageSource")
    public NacosBundleMessageSource messageSource() {
        NacosBundleMessageSource messageSource = new NacosBundleMessageSource();
        messageSource.setNacosConfigManager(this.nacosConfigManager);
        messageSource.setNacosGroup(this.property.getGroup());
        messageSource.setFileSuffix(this.property.getFileSuffix());
        messageSource.setConcurrentRefresh(this.property.getConcurrentRefresh());
        messageSource.setBasenames(this.property.getBasename());
        if (Objects.isNull(this.property.getEncoding())) {
            messageSource.setDefaultEncoding(Charset.defaultCharset().name());
        } else {
            messageSource.setDefaultEncoding(this.property.getEncoding().name());
        }
        messageSource.setFallbackToSystemLocale(this.property.isFallbackToSystemLocale());
        Duration cacheDuration = this.property.getCacheDuration();
        if (Objects.nonNull(cacheDuration)) {
            messageSource.setCacheMillis(cacheDuration.toMillis());
        }
        messageSource.setAlwaysUseMessageFormat(this.property.isAlwaysUseMessageFormat());
        messageSource.setUseCodeAsDefaultMessage(this.property.isUseCodeAsDefaultMessage());
        log.info("NacosBundleMessageSource={}", messageSource);
        return messageSource;
    }

    /**
     * 创建 I18nManager.
     *
     * @param messageSource messageSource
     * @return I18nManager
     */
    @Bean
    public I18nManager i18nManager(NacosBundleMessageSource messageSource) {
        final I18nManager i18nManager = new I18nManager(this.nacosConfigManager, messageSource, this.property);
        log.info("I18nManager={}", i18nManager);
        return i18nManager;
    }
}
